package com.ofek.applicationbase.forms;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.ofek.applicationbase.R;
import com.ofek.applicationbase.broadcast.Delegate;

public class QuestionAdapter extends ArrayAdapter<QuestionViewController> {
	private final Context context;
	private int submitRes;
	private boolean submitable;
	private FormFragment submitTo;

	public QuestionAdapter(final Context context, final ArrayList<QuestionViewController> questions) {
		super(context, 0, questions);
		this.context = context;
		submitRes = R.layout.form_submit;
	}

	public QuestionAdapter(final Context context, final List<QuestionViewController> objects) {
		super(context, 0, objects);
		this.context = context;
		submitRes = R.layout.form_submit;
	}

	public QuestionAdapter(final Context context, final FormFragment submitTo, final List<QuestionViewController> objects) {
		super(context, 0, objects);
		this.context = context;
		this.submitTo = submitTo;
		submitRes = R.layout.form_submit;
	}

	public void setSubmitResource(final int submitResource) {
		submitRes = submitResource;
	}

	@Override
	public boolean isEnabled(final int position) {
		return true;
	}

	@Override
	public boolean areAllItemsEnabled() {
		return true;
	}

	@Override
	public View getView(int position, View convertView, final ViewGroup parent) {
		if (position == getCount() - 1 && submitable) {
			final LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(submitRes, null);
			convertView.findViewById(R.id.submit).setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(final View v) {
					if (context instanceof FormActivity)
						((FormActivity) context).onSubmit();
					else
						submitTo.onSubmit();
				}
			});
			return convertView;
		} else if (position < super.getCount() || !submitable) {
			final QuestionView question = super.getItem(indices[position]).getQuestion();
			question.setLayoutParams(new ListView.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
			return question;
		} else {
			position -= super.getCount();
			if (convertView == null) {
				final LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				convertView = inflater.inflate(moreViews.get(position), null);
			}
			onViewsUsed.get(position).invoke((ViewGroup) convertView);
			return convertView;
		}

	}

	private ArrayList<Integer> moreViews;
	private ArrayList<Delegate<ViewGroup>> onViewsUsed;

	public void addView(final int viewLayout, final Delegate<ViewGroup> onViewUsed) {
		if (moreViews == null) {
			moreViews = new ArrayList<Integer>();
			onViewsUsed = new ArrayList<Delegate<ViewGroup>>();
		}
		moreViews.add(viewLayout);
		onViewsUsed.add(onViewUsed);
	}

	public void setSubmitable(final boolean submitable) {
		this.submitable = submitable;
	}

	private int[] indices;

	@Override
	public int getCount() {
		int n = super.getCount();
		indices = new int[super.getCount()];
		for (int i = 0, j = 0; i < super.getCount(); i++) {
			indices[i] = i;
			if (super.getItem(i).getQuestion().getVisibility() == View.GONE)
				n--;
			else
				indices[j++] = i;
		}
		if (submitable)
			n++;
		if (moreViews != null)
			n += moreViews.size();
		return n;
	}
}
